package vip.xiaonuo.modular.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.extension.api.R;
import org.apache.ibatis.annotations.Insert;
import vip.xiaonuo.modular.entity.dto.GPSInfoDTO;
import vip.xiaonuo.modular.entity.request.InsertLocusInfoRequest;
import vip.xiaonuo.modular.entity.response.Response;
import vip.xiaonuo.modular.service.LocusResultService;
import vip.xiaonuo.modular.util.CommonException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import vip.xiaonuo.modular.util.SHA512Util;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author tuchaoren
 * @Date 2021-06-28
 **/
@Slf4j
@Service
public class LocusResultServiceImpl implements LocusResultService {

    //    @Value("${waybill.lspKey}")
    private String lspKey = "ac59075b964b0715";

    //    @Value("${waybill.lspCode}")
    private String lspCode = "rbcc_zy";

    //    @Value("${waybill.baseUrl}")
    private String baseUrl = "https://nnbn--uat.uat1.logwirecloud.com";
    //    @Value("${waybill.insertLocusInfoUrl}")
    private String insertLocusInfoUrl = "/handler/interface/adapter";

    @Override
    public int insertLocusInfo(InsertLocusInfoRequest request) {
        // 封装请求对象
        String time = DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss");
        request.setLsp_code(lspCode);
        request.setLsp_key(lspKey);
        request.setSend_time(time);
        String sign = request.getLsp_code() + request.getVehicle_no() + request.getShipment_no() + request.getSend_time() ;
        log.info("拼接参数 -> {}", sign);
        request.setSign(SHA512Util.SHA512(sign).toUpperCase());
        log.info("回传gps信息请求参数 -> {}", JSON.toJSONString(request));
        // 发起https请求回传gps信息
        String insertLocusResult = HttpRequest.post(baseUrl + insertLocusInfoUrl).header("Content-Type", "application/json").header("protocol", "mrshipment_gps").body(JSON.toJSONString(request)).execute().body();
        // 信息对象转换
        Response parse = null;
        try{
            parse = JSONObject.parseObject(insertLocusResult, new TypeReference<Response>(){}.getType());
            log.info("回传信息 -> 条数{}，信息{}", request.getGps().size(), JSON.toJSONString(parse));

        }catch (Exception e){
            e.printStackTrace();
            log.error("回传信息异常 -> 条数{}，信息{}", parse);

        }
        if (parse == null) {
            throw new CommonException("回传轨迹时响应信息为空！");
        } else if (!"0".equals(parse.getCode())) {
            throw new CommonException(parse.getMessage());
        }
        // 回传轨迹成功！
        return 1;
    }

//    public static void main(String[] args) {
//        InsertLocusInfoRequest request = new InsertLocusInfoRequest();
//        request.setVehicle_no("粤AAG990");
//        GPSInfoDTO infoDTO = new GPSInfoDTO();
//        infoDTO.setLongitude(new BigDecimal(104.55855054217496));
//        infoDTO.setLatitude(new BigDecimal(30.246604681679464));
//        infoDTO.setDate("2021-06-29 00:00:58");
//        infoDTO.setSpeed(new BigDecimal(0));
//        request.setOrder_no("123");
//        List<GPSInfoDTO> list = new ArrayList();
//        list.add(infoDTO);
//        request.setGps(list);
//        request.setShipment_no("EDDMR21062500000001");
//        LocusResultServiceImpl locusResultService = new LocusResultServiceImpl();
//        locusResultService.insertLocusInfo(request);
//
//    }


}
